home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / wb / czesc_4 / vark15 / utils p-z / skiff2ascii.lha / iff2ascii / iff2grayascii.c < prev    next >
C/C++ Source or Header  |  1996-08-21  |  10KB  |  357 lines

  1. #include <intuition/intuition.h>
  2. #include <graphics/gfx.h>
  3. #include <dos/dos.h>
  4. #include <exec/exec.h>
  5. #include <exec/types.h>
  6. #include <diskfont/diskfont.h>
  7. #include <proto/intuition.h>
  8. #include <proto/graphics.h>
  9. #include <proto/dos.h>
  10. #include <proto/exec.h>
  11. #include <proto/diskfont.h>
  12. #include <proto/asl.h>
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. extern __asm __saveds LONG count(register __d0 a, register __d1 b);
  19.  
  20. /* Private definitions */
  21.  
  22. #define MAKE_ID(a,b,c,d)        \
  23.         ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  24.  
  25. #define ID_FORM MAKE_ID('F','O','R','M')
  26. #define ID_ILBM MAKE_ID('I','L','B','M')
  27. #define ID_BMHD MAKE_ID('B','M','H','D')
  28. #define ID_CMAP MAKE_ID('C','M','A','P')
  29. #define ID_BODY MAKE_ID('B','O','D','Y')
  30.  
  31. struct BitMapHeader
  32. {
  33.     UWORD w,h;
  34.     WORD x,y;
  35.     UBYTE nplanes;
  36.     UBYTE masking;
  37.     UBYTE compression;
  38.     UBYTE pad1;
  39.     UWORD transparentColor;
  40.     UBYTE xAspect,yAspect;
  41.     WORD pageWidth,pageHeight;
  42. };
  43.  
  44. int gw=0,gh=0;
  45.  
  46. int ReadILBM(char *fname, struct BitMap **bmp, ULONG *palette)
  47. {
  48.     FILE *f;
  49.  
  50.     ULONG nam,siz,remain;
  51.     BOOL BMHDfl=FALSE,CMAPfl=FALSE,BODYfl=FALSE;
  52.     struct BitMapHeader bmhd;
  53.     int bmwidth,bmheight,bmdepth;
  54.  
  55.     if(!(f=fopen(fname,"rb")))
  56.         return 0;
  57.  
  58.     if(!fread(&nam,sizeof(ULONG),1,f))
  59.     {
  60.         fclose(f);
  61.         return 0;
  62.     }
  63.  
  64.     if(nam!=ID_FORM)
  65.     {
  66.         fclose(f);
  67.         return 0;
  68.     }
  69.  
  70.     if(!fread(&remain,sizeof(ULONG),1,f))
  71.     {
  72.         fclose(f);
  73.         return 0;
  74.     }
  75.  
  76.     if(!fread(&nam,sizeof(ULONG),1,f))
  77.     {
  78.         fclose(f);
  79.         return 0;
  80.     }
  81.  
  82.     if(nam!=ID_ILBM)
  83.     {
  84.         fclose(f);
  85.         return 0;
  86.     }
  87.  
  88.     remain-=4;
  89.  
  90.     /* Chunk reading loop */
  91.     while(remain>0 && !feof(f) && !(BMHDfl && BODYfl && (CMAPfl || !palette)))
  92.     {
  93.         if(!fread(&nam,sizeof(ULONG),1,f))
  94.         {
  95.             fclose(f);
  96.             return 0;
  97.         }
  98.  
  99.         if(!fread(&siz,sizeof(ULONG),1,f))
  100.         {
  101.             fclose(f);
  102.             return 0;
  103.         }
  104.  
  105.         remain-=8+siz+(siz & 1);
  106.  
  107.         switch(nam)
  108.         {
  109.             case ID_BMHD:
  110.                 if(siz!=sizeof(struct BitMapHeader))
  111.                 {
  112.                     fclose(f);
  113.                     return 0;
  114.                 }
  115.  
  116.                 if(!fread(&bmhd,sizeof(struct BitMapHeader),1,f))
  117.                 {
  118.                     fclose(f);
  119.                     return 0;
  120.                 }
  121.                 BMHDfl=TRUE;
  122.                 gw=bmwidth=bmhd.w;
  123.                 gh=bmheight=bmhd.h;
  124.                 bmdepth=bmhd.nplanes;
  125.  
  126.                 if(bmdepth!=1)
  127.                 {
  128.                     fclose(f);
  129.                     return 0;
  130.                 }
  131.                 break;
  132.             case ID_BODY:
  133.                 if(!BMHDfl)
  134.                 {
  135.                     fclose(f);
  136.                     return 0;
  137.                 }
  138.                 *bmp=AllocBitMap(bmwidth,bmheight,bmdepth,BMF_CLEAR /*|BMF_DISPLAYABLE*/ /*|BMF_INTERLEAVED*/,NULL);
  139.                 if(!*bmp)
  140.                 {
  141.                     fclose(f);
  142.                     return 0;
  143.                 }
  144.  
  145.                 if(bmhd.compression)
  146.                 {
  147.                     int i,j,k,l;
  148.                     UBYTE *pptr;
  149.                     BYTE sel;
  150.                     for(i=0;i<bmheight;i++) /* Line */
  151.                     {
  152.                         for(j=0;j<bmdepth;j++) /* Plane */
  153.                         {
  154.                             pptr=(*bmp)->Planes[j]+(*bmp)->BytesPerRow*i;
  155.                             k=2*((bmwidth+15)>>4);
  156.                             while(k)
  157.                             {
  158.                                 sel=fgetc(f);
  159.                                 l=(int)sel;
  160.                                 if(l>=0 && l<=127)
  161.                                 {
  162.                                     l++;
  163.                                     k-=l;
  164.                                     while(l--)
  165.                                         *pptr++=fgetc(f);
  166.                                 }
  167.                                 else if(l>=-127 && l<=-1)
  168.                                 {
  169.                                     l=1-l;
  170.                                     k-=l;
  171.                                     sel=fgetc(f);
  172.                                     while(l--)
  173.                                         *pptr++=sel;
  174.                                 }
  175.                             }
  176.                         }
  177.                     }
  178.                 }
  179.                 else
  180.                 {
  181.                     int i,j,k;
  182.                     UBYTE *pptr;
  183.                     for(i=0;i<bmheight;i++) /* Line */
  184.                     {
  185.                         for(j=0;j<bmdepth;j++) /* Plane */
  186.                         {
  187.                             pptr=(*bmp)->Planes[j]+(*bmp)->BytesPerRow*i;
  188.                             for(k=0;k<2*((bmwidth+15)>>4);k++) /* Byte */
  189.                             {
  190.                                 *pptr++=fgetc(f);
  191.                             }
  192.                         }
  193.                     }
  194.                 }
  195.                 if(siz&1)
  196.                         fseek(f,1,SEEK_CUR);
  197.                 BODYfl=TRUE;
  198.                 break;
  199.             case ID_CMAP:
  200.                 if(!palette)
  201.                 {
  202.                     if(siz&1)
  203.                         siz++;
  204.                     fseek(f,siz,SEEK_CUR);
  205.                 }
  206.                 else
  207.                 {
  208.                     UBYTE r,g,b;
  209.                     int i;
  210.  
  211.                     palette[0]=(siz/3)<<16;
  212.  
  213.                     for(i=0;i<siz/3;i++)
  214.                     {
  215.                         r=fgetc(f);
  216.                         g=fgetc(f);
  217.                         b=fgetc(f);
  218.                         palette[i*3+1]=MAKE_ID(r,r,r,r);
  219.                         palette[i*3+2]=MAKE_ID(g,g,g,g);
  220.                         palette[i*3+3]=MAKE_ID(b,b,b,b);
  221.                     }
  222.  
  223.                     palette[i*3+1]=0;
  224.  
  225.                     if(siz&1)
  226.                         fseek(f,1,SEEK_CUR);
  227.                 }
  228.                 CMAPfl=TRUE;
  229.                 break;
  230.             default:
  231.                 if(siz&1)
  232.                     siz++;
  233.                 fseek(f,siz,SEEK_CUR);
  234.                 break;
  235.         }
  236.     }
  237.  
  238.     fclose(f);
  239.     return BMHDfl && BODYfl;
  240. }
  241.  
  242. int main(int argc, char *argv[])
  243. {
  244.     struct BitMap *imgbm, *fntbm;
  245.     struct RastPort imgrp,fntrp;
  246.     struct TextFont *fnt;
  247.     static struct TextAttr ta=
  248.     {
  249.         NULL,
  250.         8,
  251.         FS_NORMAL,
  252.         0
  253.     };
  254.     char chrs[193];
  255.     int i,x,y;
  256.     int mej,cuan;
  257.     int cuaneste;
  258.     int y2;
  259.     int mchar=192;
  260.     int gris[192],mgris=0;
  261.     
  262.     UBYTE *p1,*p2,*p3;
  263.  
  264.     if(argc!=4 || (argv[3][0]!='8' && argv[3][0]!='7'))
  265.     {
  266.         printf("Usage: %s iff-file fontname.font {7|8} >output-file\n",argv[0]);
  267.         exit(5);
  268.     }
  269.     
  270.     if(argv[3][0]=='7')
  271.     {
  272.         mchar=96;
  273.     }
  274.     
  275.     ta.ta_Name=argv[2];
  276.     
  277.     if(fnt=OpenDiskFont(&ta))
  278.     {
  279.         if(fntbm=AllocBitMap(192*8,8,1,BMF_CLEAR /*|BMF_DISPLAYABLE*/,NULL))
  280.         {
  281.             InitRastPort(&fntrp);
  282.             fntrp.BitMap=fntbm;
  283.             
  284.             if(ReadILBM(argv[1],&imgbm,NULL))
  285.             {
  286.                 InitRastPort(&imgrp);
  287.                 imgrp.BitMap=imgbm;
  288.                 
  289.                 SetFont(&fntrp,fnt);
  290.                 
  291.                 for(i=0;i<192;i++)
  292.                 {
  293.                     chrs[i]=32+i+32*(i>=96);
  294.                 }
  295.                 chrs[192]=0;
  296.                 
  297.                 SetAPen(&fntrp,1);
  298.                 SetBPen(&fntrp,0);
  299.                 Move(&fntrp,0,fnt->tf_Baseline);
  300.                 Text(&fntrp,chrs,192);
  301.                 
  302.                 for(i=0;i<mchar;i++)
  303.                 {
  304.                     cuaneste=0;
  305.                     p2=fntbm->Planes[0]+i;
  306.                     
  307.                     for(y2=0;y2<8;y2++)
  308.                     {
  309.                         cuaneste+=count(0,*p2);
  310.                         p2+=fntbm->BytesPerRow;
  311.                     }
  312.                     gris[i]=cuaneste;
  313.                     if(cuaneste>mgris)
  314.                         mgris=cuaneste;
  315.                 }
  316.                 
  317.                 gw/=8;
  318.                 gh/=8;
  319.                 
  320.                 if(gw && gh)
  321.                 {
  322.                     for(y=0;y<gh;y++)
  323.                     {
  324.                         p1=imgbm->Planes[0]+y*8*imgbm->BytesPerRow;
  325.                         for(x=0;x<gw;x++)
  326.                         {
  327.                             mej=0;
  328.                             cuan=0;
  329.                             
  330.                             p3=p1;
  331.                             for(y2=0;y2<8;y2++)
  332.                             {
  333.                                 cuan+=count(*p3,0);
  334.                                 p3+=imgbm->BytesPerRow;
  335.                             }
  336.                             cuan=(cuan*mgris)/64;
  337.                             for(i=1;i<mchar && cuan;i++)
  338.                             {
  339.                                 if((gris[i]-cuan)*(gris[i]-cuan)<(gris[mej]-cuan)*(gris[mej]-cuan))
  340.                                 {
  341.                                     mej=i;
  342.                                 }
  343.                             }
  344.                             printf("%c",32+mej+32*(mej>=96));
  345.                             p1++;
  346.                         }
  347.                         printf("\n");
  348.                     }
  349.                 }
  350.                 
  351.                 FreeBitMap(imgbm);
  352.             }
  353.             FreeBitMap(fntbm);
  354.         }
  355.         CloseFont(fnt);
  356.     }
  357. }